<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Numeric Types and Type Conversions
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot043.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot042.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot045.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc112">8.2</A>&nbsp;&nbsp;Numeric Types and Type Conversions</H2><UL>
<LI><A HREF="umsroot044.html#toc70">Integers</A>
<LI><A HREF="umsroot044.html#toc71">Rationals</A>
<LI><A HREF="umsroot044.html#toc72">Floating Point Numbers</A>
<LI><A HREF="umsroot044.html#toc73">Bounded Real Numbers</A>
<LI><A HREF="umsroot044.html#toc74">Type Conversions</A>
</UL>

<A NAME="@default395"></A>
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> distinguishes four types of numbers: <B>integers</B>,
<B>rationals</B>, <B>floats</B> and <B>bounded reals</B>.
<BR>
<BR>
<A NAME="toc70"></A>
<H3 CLASS="subsection"><A NAME="htoc113">8.2.1</A>&nbsp;&nbsp;Integers</H3>
<A NAME="intrep"></A>
<A NAME="@default396"></A>
<A NAME="@default397"></A>
<A NAME="@default398"></A>
The magnitude of integers is only limited by your available 
memory. However, integers that fit into the word size of your computer are
represented more efficiently (this distinction is invisible to the user).
Integers are written in decimal notation or in base notation, e.g.:
<PRE CLASS="verbatim">
 0  3  -5  1024  16'f3ae  0'a  15511210043330985984000000
</PRE>
Note that integer range is unlimited if ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> was compiled with
bignum support. Otherwise, integers are restricted to that representable
in a machine word, and <TT>max_integer flag</TT> of <A HREF="../bips/kernel/env/get_flag-2.html"><B>get_flag/2</B></A><A NAME="@default399"></A> 
returns the maximum integer value. <BR>
<BR>
<A NAME="toc71"></A>
<H3 CLASS="subsection"><A NAME="htoc114">8.2.2</A>&nbsp;&nbsp;Rationals</H3>
<A NAME="@default400"></A>
<A NAME="@default401"></A>
Rational numbers implement the corresponding mathematical domain,
i.e. ratios of two integers (numerator and denominator).
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> represents rationals in a canonical form where the 
greatest common divisor of numerator and denominator is 1 and the
denominator is positive. Rational constants are written as numerator
and denominator separated by an underscore, e.g.
<PRE CLASS="verbatim">
 1_3  -30517578125_32768  0_1
</PRE>Rational arithmetic is arbitrarily precise. When the global flag
<A NAME="@default402"></A>
<A NAME="@default403"></A>
<TT>prefer_rationals</TT> is set, the system uses rational arithmetic
wherever possible. In particular, dividing two integers then yields a precise
rational rather than a float result.<BR>
<BR>
Rationals are supported if ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> is compiled with bignum support. 
If rationals are not supported, a type error will be raised when a rational is 
required.<BR>
<BR>
<A NAME="toc72"></A>
<H3 CLASS="subsection"><A NAME="htoc115">8.2.3</A>&nbsp;&nbsp;Floating Point Numbers</H3>
<A NAME="@default404"></A>
<A NAME="@default405"></A>
Floating point numbers conceptually correspond to the mathematical
domain of real numbers, but are not precisely represented.
Floats are written with decimal point and/or an exponent, e.g.
<PRE CLASS="verbatim">
 0.0  3.141592653589793  6.02e23  -35e-12  -1.0Inf
</PRE><A NAME="@default406"></A>
<A NAME="@default407"></A>
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> uses double precision floats<SUP><A NAME="text8" HREF="umsroot042.html#note8">1</A></SUP>.<BR>
<BR>
<A NAME="toc73"></A>
<H3 CLASS="subsection"><A NAME="htoc116">8.2.4</A>&nbsp;&nbsp;Bounded Real Numbers</H3>
<A NAME="@default408"></A>
<A NAME="@default409"></A>
<A NAME="@default410"></A>
It is a well known problem that floating point arithmetic suffers
from rounding errors.
To provide safe arithmetic over the <B>real</B> numbers, ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>
also implements <B>bounded reals</B><SUP><A NAME="text9" HREF="umsroot042.html#note9">2</A></SUP>.
A bounded real consists of a pair of floating point numbers
which constitute a safe lower and upper bound for the real number that
is being represented. Bounded reals are written as two floating point
numbers separated by two underscores, e.g.
<PRE CLASS="verbatim">
 -0.001__0.001  3.141592653__3.141592654  1e308__1.0Inf
</PRE>A bounded real is a representation for a real number that definitely lies
somewhere between the two bounds, but the exact value cannot be determined
<SUP><A NAME="text10" HREF="umsroot042.html#note10">3</A></SUP>.
Bounded reals are usually not typed in by the user, they are normally
the result of a computation or type coercion.<BR>
<BR>
All computations with bounded reals give safe results, taking rounding
errors into account. This is achieved by doing interval arithmetic
<A NAME="@default411"></A>
on the bounds and rounding the results outwards. The resulting
bounded real is then guaranteed to enclose the true real result.<BR>
<BR>
Computations with floating point values result in uncertainties
about the correct result. Bounded reals make this uncertainty
explicit. A consequence of this is that sometimes it is conceptually
not possible to decide whether two bounded reals are identical or not.
This occurs when the bounds of the compared intervals overlap.
In this case, the arithmetic comparisons leave a (ground) delayed goal
behind which can then be inspected by the user to decide whether the
match is considered close enough. The syntactial comparisons like
<A HREF="../bips/kernel/termcomp/E-2.html"><B>=/2</B></A><A NAME="@default412"></A> and
<A HREF="../bips/kernel/termcomp/EE-2.html"><B>==/2</B></A><A NAME="@default413"></A> treat bounded reals
simply as a pair of bounds, and consider them equal when the bounds are
equal.<BR>
<BR>
<A NAME="toc74"></A>
<H3 CLASS="subsection"><A NAME="htoc117">8.2.5</A>&nbsp;&nbsp;Type Conversions</H3>
Note that numbers of different types never unify, e.g. 3, 3_1, 3.0
and 3.0__3.0 are all different.
Use the arithmetic comparison predicates when you want to
compare numeric values.
When numbers of different types occur as arguments of an arithmetic
operation or comparison, the types are first made equal by converting
to the more general of the two types, i.e. the rightmost one in the sequence
<BLOCKQUOTE CLASS="quote">
integer &rarr; rational &rarr; float &rarr; bounded real
</BLOCKQUOTE>
The operation or comparison is then carried out with this type and the
result is of this type as well, unless otherwise specified.
Beware of the potential loss of precision in the
rational &rarr; float conversion!
Note that the system never does automatic conversions in the opposite direction.
Such conversion must be programmed explicitly using the
<A HREF="../bips/kernel/arithmetic/integer-2.html"><B>integer</B></A><A NAME="@default414"></A>,
<A HREF="../bips/kernel/arithmetic/rational-2.html"><B>rational</B></A><A NAME="@default415"></A>,
<A HREF="../bips/kernel/arithmetic/float-2.html"><B>float</B></A><A NAME="@default416"></A> and
<A HREF="../bips/kernel/arithmetic/breal-2.html"><B>breal</B></A><A NAME="@default417"></A>
functions.<BR>
<BR>
<HR>
<A HREF="umsroot043.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot042.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot045.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
